//
//  RYYuShengzhangHead.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/13.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class RYYuShengzhangHead: SYBaseView , UICollectionViewDelegate, UICollectionViewDataSource{
    
    var collectionView: UICollectionView!
    
    var datas = [Sensors]()
    

    public func configureView(_ models: [Sensors]) {
        self.datas = models
        collectionView.reloadData()
        if models.count > 3 {
            
            collectionView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(10.autoWidth())
                make.left.equalToSuperview().offset(12.autoWidth())
                make.right.equalToSuperview().offset(-12.autoWidth())
                make.height.equalTo(210.0.autoWidth())
            }
        }
    }
    
    override func initViews() {
        
        self.backgroundColor = .white
        setCollectionView()
        
        self.addSubview(collectionView)
        self.addSubview(lineView)
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(10.autoWidth())
            make.left.equalToSuperview().offset(12.autoWidth())
            make.right.equalToSuperview().offset(-12.autoWidth())
            make.height.equalTo(110.0.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-10.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(0.5.autoWidth())
        }
        
    }
    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 85.0.autoWidth()
        layout.itemSize = CGSize(width: itemWidth, height: 105.autoWidth())
        layout.minimumLineSpacing = 1.autoWidth()
        layout.minimumInteritemSpacing = 47.autoWidth()
        layout.scrollDirection = .vertical
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        collectionView.isScrollEnabled = false
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(RYYuShengzhangTuCell.self, forCellWithReuseIdentifier: NSStringFromClass(RYYuShengzhangTuCell.self))
        self.addSubview(self.collectionView)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: RYYuShengzhangTuCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(RYYuShengzhangTuCell.self), for: indexPath) as? RYYuShengzhangTuCell
        cell?.configureView(datas[indexPath.row])
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
    }
    
    
    @objc func chakanAction() {
        
    }
}



class RYYuShengzhangTuCell: SYBaseCollectionViewCell {
    
    let wenLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x7B7B7B)
        v.font = 12.autoFontSize()
        
        v.textAlignment = .center
        return v
    }()
    
    let suzhiLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x367FF6)
        v.font = 19.autoFontSize()
        
        v.textAlignment = .center
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        
        return v
    }()
    

    public func configureView(_ model: Sensors) {
        
        switch model.typeCode {
        case "AIR_TEMPERATURE":
//            wenLb.text = "空气温度"
            pic.image = UIImage(named: "kongqi_wendu")
        case "AIR_HUMIDITY":
//            wenLb.text = "空气湿度"
            pic.image = UIImage(named: "kongqi_shidu")
        case "AIR_ILLUMINATION":
//            wenLb.text = "光照"
            pic.image = UIImage(named: "guangzhao")
        case "SOIL_TEMPERATURE":
//            wenLb.text = "土壤温度"
            pic.image = UIImage(named: "turang_wendu")
        case "SOIL_HUMIDITY":
//            wenLb.text = "土壤温度"
            pic.image = UIImage(named: "turang_shidu")
        case "AIR_CO2":
//            wenLb.text = "二氧化碳"
            pic.image = UIImage(named: "eryanghuatan")
        case "PH":
//            wenLb.text = "PH值"
            pic.image = UIImage(named: "ph")
        case "DISSOLVED_O2":
//            wenLb.text = "溶解氧"
            pic.image = UIImage(named: "rongjieyang")
        case "WATER_TEMPERATURE":
//            wenLb.text = "[水]-温度"
            pic.image = UIImage(named: "shuiwen")
        default:
            break
        }
        wenLb.text = "\(model.deviceName ?? "")(\(model.unit ?? ""))"
        suzhiLb.text = model.currentValue
    }
    
    override func initViews() {
        
        addSubview(wenLb)
        addSubview(pic)
        addSubview(suzhiLb)
        wenLb.snp.makeConstraints { (make) in
            make.top.equalTo(pic.snp.bottom).offset(3.autoWidth())
            make.centerX.equalToSuperview()
            make.height.equalTo(13.autoWidth())
            make.bottom.equalToSuperview()
        }
        pic.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(85.autoWidth())
            make.width.equalTo(85.autoWidth())
        }
        suzhiLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(45.autoWidth())
            make.centerX.equalToSuperview()
            make.height.equalTo(15.autoWidth())
        }
    }
}


